from flask import Flask, render_template, request, redirect, url_for, session, g, jsonify
import config
from models import *
from exts import db
from sqlalchemy import or_

app = Flask(__name__)
app.config.from_object(config)
db.init_app(app)


@app.route('/')
def index():
    return render_template('index.html')


@app.route('/login/', methods=['POST', 'GET'])
def login():
    mobile = request.form.get('mobile')
    password = request.form.get('password')
    print(mobile + " " + password)
    user = User.query.filter(mobile == User.mobile).first()

    if (user and user.password == password):
        session['user_id'] = user.id
        session.permanent = True
        return redirect(url_for('index'))
    else:
        return jsonify(msg=0)


@app.before_request
def web_before_request():
    user_id = session.get('user_id')
    if (user_id):
        user = User.filter(User.id == user_id).first()
        if (user):
            g.user = user



if __name__ == '__main__':
    print(app.url_map)
    app.run()
